<!--
    THIS EXAMPLE WAS DOWNLOADED FROM https://echarts.apache.org/examples/zh/editor.html?c=bar-race-country
-->
<!DOCTYPE html>
<html style="height: 100%">
    <head>
        <meta charset="utf-8-sig">
    </head>
    <body style="height: 100%; margin: 0">
        <div id="container" style="height: 50%;"></div>
        <div id="dynamicBar" style="width:700px;height:500px;"></div>
        <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
<!--dynamicBar(横)-->
        <script type="text/javascript">
var dom = document.getElementById("dynamicBar");
var myChart = echarts.init(dom);
var data1 = {{ data|safe }};
//var data2 = {{ flags|safe }};
var app = {};
var option;
var updateFrequency = 2000;
var dimension = 0;
var countryColors = {
        中国: '#FF5E4F',
        加拿大: '#45AD94',
        日本: '#4D57FF',
        西班牙: '#969498',
        俄罗斯: '#45ACEC',
        法国: '#A7AA62',
        意大利: '#EC9795',
        印度: '#DE9E75',
        墨西哥: '#C49E60',
        美国: '#47984B',
        英国: '#DD9CD1',
        澳大利亚: '#92B668',
        韩国: '#B2A6EC',
        巴西: '#44AFBD',
        德国: '#89ADF1'
      };
    var flags =  [
  { emoji: "🇨🇳", name: "中国" },
  { emoji: "🇨🇦", name: "加拿大" },
  { emoji: "🇯🇵", name: "日本" },
  { emoji: "🇪🇸", name: "西班牙" },
  { emoji: "🇷🇺", name: "俄罗斯" },
  { emoji: "🇫🇷", name: "法国" },
  { emoji: "🇮🇹", name: "意大利" },
  { emoji: "🇮🇳", name: "印度" },
  { emoji: "🇲🇽", name: "墨西哥" },
  { emoji: "🇺🇸", name: "美国" },
  { emoji: "🇬🇧", name: "英国" },
  { emoji: "🇦🇺", name: "澳大利亚" },
  { emoji: "🇰🇷", name: "韩国" },
  { emoji: "🇧🇷", name: "巴西" },
  { emoji: "🇩🇪", name: "德国" },
];

    var data = data1;

    var years = [];
    for (var i = 0; i < data.length; ++i) {
        if (years.length === 0 || years[years.length - 1] !== data[i][4]) {
            years.push(data[i][4]);
        }
    }
    console.log(years);
    function getFlag(countryName) {
        if (!countryName) {
            return '';
        }
        return (flags.find(function (item) {
            return item.name === countryName;
        }) || {}).emoji;
    }
    var startIndex = 1;
    var startYear = years[startIndex];

    var option = {
        grid: {
            top: 10,
            bottom: 30,
            left: 150,
            right: 80
        },
        xAxis: {
            max: 'dataMax',
            label: {
                formatter: function (n) {
                    return Math.round(n);
                }
            }
        },
        dataset: {
            source: data.slice(1).filter(function (d) {
                return d[4] === startYear;
            })
        },
        yAxis: {
            type: 'category',
            inverse: true,
            max: 10,
            axisLabel: {
                show: true,
                textStyle: {
                    fontSize: 14
                },
                formatter: function (value) {
                    return value + '{flag|' + getFlag(value) + '}';
                },
                rich: {
                    flag: {
                        fontSize: 25,
                        padding: 5
                    }
                }
            },
            animationDuration: 300,
            animationDurationUpdate: 300
        },
        series: [{
            realtimeSort: true,
            seriesLayoutBy: 'column',
            type: 'bar',
            itemStyle: {
                color: function (param) {
                    return countryColors[param.value[3]] || '#5470c6';
                }
            },
            encode: {
                x: dimension,
                y: 3
            },
            label: {
                show: true,
                precision: 1,
                position: 'right',
                valueAnimation: true,
                fontFamily: 'monospace'
            }
        }],
        // Disable init animation.
        animationDuration: 0,
        animationDurationUpdate: updateFrequency,
        animationEasing: 'linear',
        animationEasingUpdate: 'linear',
        graphic: {
            elements: [{
                type: 'text',
                right: 160,
                bottom: 60,
                style: {
                    text: startYear,
                    font: 'bolder 80px monospace',
                    fill: 'rgba(100, 100, 100, 0.25)'
                },
                z: 100
            }]
        }
    };

    // console.log(option);
    myChart.setOption(option);

    for (var i = startIndex; i < years.length - 1; ++i) {
        (function (i) {
            setTimeout(function () {
                updateYear(years[i + 1]);
            }, (i - startIndex) * updateFrequency);
        })(i);
    }

    function updateYear(year) {
        var source = data.slice(1).filter(function (d) {
            return d[4] === year;
        });
        option.series[0].data = source;
        option.graphic.elements[0].style.text = year;
        myChart.setOption(option);
    }


if (option && typeof option === 'object') {
    myChart.setOption(option);
}

        </script>
    </body>
</html>
